package com.kcwl.carrier.infrastructure.repository.persostence;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcwl.carrier.domain.entity.VehicleBlacklistCondition;
import com.kcwl.carrier.domain.entity.VehicleBlacklistInfo;
import com.kcwl.carrier.domain.repository.IVehicleBlacklistRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.VehicleBlacklistMapper;
import com.kcwl.carrier.infrastructure.repository.po.VehicleBlacklistPO;
import com.kcwl.carrier.interfaces.query.VehicleBlacklistQuery;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.CollectionUtil;
import com.kcwl.framework.utils.KcBeanConverter;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class VehicleBlacklistRepositoryImpl extends BaseRepositoryImpl<VehicleBlacklistMapper, VehicleBlacklistPO> implements IVehicleBlacklistRepository {

    @Resource
    private VehicleBlacklistMapper vehicleBlacklistMapper;


    @Override
    public List<VehicleBlacklistInfo> queryVehicleBlacklistList(VehicleBlacklistCondition condition) {
        QueryWrapper<VehicleBlacklistPO> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getId()), "id", condition.getId());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getVehicleId()), "vehicle_id", condition.getVehicleId());
        wrapper.eq(StringUtil.isNotEmpty(condition.getPlateNumber()), "plate_number", condition.getPlateNumber());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getReasonCode()), "reason_code", condition.getReasonCode());
        wrapper.like(StringUtil.isNotEmpty(condition.getReason()), "reason", condition.getReason());
        wrapper.like(StringUtil.isNotEmpty(condition.getRemark()), "remark", condition.getRemark());
        wrapper.like(StringUtil.isNotEmpty(condition.getCreateName()), "create_name", condition.getCreateName());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getIsDel()), "is_del", condition.getIsDel());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getGrade()), "grade", condition.getGrade());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getCreateUserId()), "create_user_id", condition.getCreateUserId());
        wrapper.like(ObjectUtil.isNotEmpty(condition.getUpdateName()), "update_name", condition.getUpdateName());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getUpdateUserId()), "update_user_id", condition.getUpdateUserId());
        wrapper.in(!CollectionUtil.isEmpty(condition.getVehicleIds()),"vehicle_id",condition.getVehicleIds());
        List<VehicleBlacklistPO> vehicleBlacklistPOS = vehicleBlacklistMapper.selectList(wrapper);
        return KcBeanConverter.toList(vehicleBlacklistPOS, VehicleBlacklistInfo.class);
    }

    @Override
    public KcPage<VehicleBlacklistInfo> queryVehicleBlacklistPage(VehicleBlacklistQuery query) {
        QueryWrapper<VehicleBlacklistPO> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(query.getId()), "id", query.getId());
        wrapper.eq(ObjectUtil.isNotEmpty(query.getVehicleId()), "vehicle_id", query.getVehicleId());
        wrapper.eq(StringUtil.isNotEmpty(query.getPlateNumber()), "plate_number", query.getPlateNumber());
        wrapper.eq(ObjectUtil.isNotEmpty(query.getReasonCode()), "reason_code", query.getReasonCode());
        wrapper.like(StringUtil.isNotEmpty(query.getReason()), "reason", query.getReason());
        wrapper.like(StringUtil.isNotEmpty(query.getRemark()), "remark", query.getRemark());
        wrapper.like(StringUtil.isNotEmpty(query.getCreateName()), "create_name", query.getCreateName());
        wrapper.eq(ObjectUtil.isNotEmpty(query.getIsDel()), "is_del", query.getIsDel());
        wrapper.eq(ObjectUtil.isNotEmpty(query.getGrade()), "grade", query.getGrade());
        wrapper.eq(ObjectUtil.isNotEmpty(query.getCreateUserId()), "create_user_id", query.getCreateUserId());
        wrapper.like(ObjectUtil.isNotEmpty(query.getUpdateName()), "update_name", query.getUpdateName());
        wrapper.eq(ObjectUtil.isNotEmpty(query.getUpdateUserId()), "update_user_id", query.getUpdateUserId());
        wrapper.in(!CollectionUtil.isEmpty(query.getVehicleIds()),"vehicle_id",query.getVehicleIds());
        IPage<VehicleBlacklistPO> iPage = vehicleBlacklistMapper.selectPage(new Page<>(query.getCurPagerNo(), query.getPageSize()), wrapper);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public Long addBlacklist(VehicleBlacklistPO vehicleBlacklistPO) {
        return (long) vehicleBlacklistMapper.insert(vehicleBlacklistPO);
    }

    @Override
    public Class<VehicleBlacklistPO> getPoClass() {
        return VehicleBlacklistPO.class;
    }
}
